package main;

import jade.core.Profile;
import jade.core.ProfileImpl;
import jade.core.Runtime;
import jade.wrapper.AgentContainer;
import jade.wrapper.AgentController;
import jade.wrapper.ControllerException;
import jade.wrapper.StaleProxyException;

import java.util.ArrayList;
import java.util.List;

import BuyerAgent.Buyer;

/**
 * JADE runtime and main container starter which supports the launcher.
 * 
 * @author DUY HAN PHAN
 * 
 */
public class RunTimeStarter {
	private AgentContainer mainContainer;
	private Runtime rt;

	public RunTimeStarter() {
		// Get a hold on JADE runtime
		rt = Runtime.instance();

		// Exit the JVM when there are no more containers around
		rt.setCloseVM(true);

		// Start Main Container
		Profile pMain = new ProfileImpl(null, 9999, null);
		mainContainer = rt.createMainContainer(pMain);
		try {
			mainContainer.start();
		} catch (ControllerException e) {
			e.printStackTrace();
		}
	}

	public void showJadeGui() {
		try {
			System.out.println("Launching the rma agent on the main container ...");
			AgentController rma = mainContainer.createNewAgent("rma", "jade.tools.rma.rma", new Object[0]);
			rma.start();
		} catch (StaleProxyException e) {
			e.printStackTrace();
		}
	}

	public void startBuyerAgents(int numBuyers) {
		// CREATE ALL BUYER AGENTS WHICH ARE INITIALLY LOCATED IN DIFFERENT
		// CONTAINERS
		for (int i = 1; i <= numBuyers; i++) {
			try {
				int numCurrentBuyers = MainFrame.runningBuyerList.size();
				numCurrentBuyers = numCurrentBuyers + 1;
				String buyerName = "BuyerAgent-" + numCurrentBuyers;
				if (MainFrame.isRunningBuyer(buyerName)) {
					continue;
				}
				MainFrame.runningBuyerList.add(buyerName);
				// create container
				Profile p = new ProfileImpl(false);
				p.setParameter(Profile.CONTAINER_NAME, "Container-" + numCurrentBuyers);
				AgentContainer agentContainer = rt.createAgentContainer(p);
				agentContainer.start();
				String buyerAgentName = buyerName;
				ArrayList<String> argsList = new ArrayList<String>();
				argsList.add(buyerAgentName);
				// create an instance of buyer agent
				AgentController buyerAgent = agentContainer.createNewAgent(buyerAgentName, Buyer.class.getName(), null);
				buyerAgent.start();
			} catch (ControllerException e) {
				e.printStackTrace();
			}
		}
	}

	public void startSellerAgents(int numSellers) {
		try {
			// start all seller agents in main container
			List<String> arguments = new ArrayList<String>();
			// String NO_GUI = "No GUI";
			// arguments.add(NO_GUI);
			for (int i = 1; i <= numSellers; i++) {
				int numCurrentSellers = MainFrame.runningSellerList.size();
				numCurrentSellers = numCurrentSellers + 1;
				String sellerName = "SellerAgent-" + numCurrentSellers;
				if (MainFrame.isRunningSeller(sellerName)) {
					continue;
				}
				MainFrame.runningSellerList.add(sellerName);
				AgentController seller = mainContainer.createNewAgent(sellerName, "SellerAgent.Seller",
						arguments.toArray());
				seller.start();
			}
		} catch (ControllerException e) {
			e.printStackTrace();
		}
	}
}
